package com.board.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.board.dao.OrderLogMapper;
import com.board.entity.OrderLog;
import com.board.exception.OrderLogException;
import com.board.param.FindOrderLogParam;
import com.board.service.OrderLogService;
import com.board.util.PageInfo;
import org.aspectj.weaver.ast.Or;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 膳食系统-订单日志表 服务实现类
 * </p>
 *
 * @author 小叶
 * @since 2024年04月04日
 */
@Service
public class OrderLogServiceImpl extends ServiceImpl<OrderLogMapper, OrderLog> implements OrderLogService {
    @Autowired
    private OrderLogMapper orderLogMapper;

    @Override
    public OrderLog findOrderLogById(Integer logId) {
        OrderLog orderLog = orderLogMapper.selectById(logId);
        if (orderLog==null) throw new OrderLogException(500,"未查询到该订单日志");
        return orderLog;
    }

    @Override
    public PageInfo<OrderLog> findOrderLogByCondition(FindOrderLogParam param) {
        //订单类型只能是0或1
        if (param.getOrderType()!=0&&param.getOrderType()!=1){
            throw new OrderLogException(500,"订单类型错误");
        }
        QueryWrapper<OrderLog> wrapper = new QueryWrapper<>();
        if (param.getLogUser()!=null&&param.getLogUser()!=""){
            wrapper.like("log_user",param.getLogUser());
        }
        if (param.getOrderNum()!=null&&param.getOrderNum()!=""){
            wrapper.like("order_num",param.getOrderNum());
        }
        if (param.getOrderType()!=null){
            wrapper.eq("order_type",param.getOrderType());
        }
        Page<OrderLog> page = new Page<>(param.getPageNum()==null?1:param.getPageNum(),param.getPageSize()==null?5:param.getPageSize());
        orderLogMapper.selectPage(page,wrapper);
        PageInfo pageInfo = BeanUtil.toBean(page, PageInfo.class);
        pageInfo.setPages(page.getPages());
        return pageInfo;
    }
}
